package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.CompanyAd;
import com.jyy.prefabricated.entity.ProjectInfo;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.CompanyAdMapper;
import com.jyy.prefabricated.mapper.ProjectInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@Service
@Transactional
public class CompanyAdService {

    @Autowired
    private CompanyAdMapper companyAdMapper;

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    /**
     * 新增
     * @param companyAd
     * @return
     */
    public Integer insert(CompanyAd companyAd){
        companyAdMapper.insert(companyAd);
        return companyAd.getId();
    }

    /**
     * 更新
     * @param companyAd
     * @return
     */
    public int update(CompanyAd companyAd){
        if(companyAd.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return companyAdMapper.updateByPrimaryKeySelective(companyAd);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return companyAdMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public CompanyAd selectById(Integer id){
        return companyAdMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据机构id查询企业风采广告信息
     * @param trainingOrganizationId
     * @param position
     * @param status
     * @return
     */
    public List<CompanyAd> selectByTo(Integer trainingOrganizationId, Integer position, Integer status){
        return companyAdMapper.selectByTo(trainingOrganizationId, position, status);
    }

    /**
     * 查询待展示的企业广告信息
     * @param trainingOrganizationId
     * @param position
     * @return
     */
    public List<CompanyAd> selectAdByWaitStart(Integer trainingOrganizationId, Integer position){
        return companyAdMapper.selectAdByWaitStart(trainingOrganizationId, position);
    }

    /**
     * 根据参数查询列表
     * @param position 广告位置
     * @param status 状态
     * @return
     */
    public List<CompanyAd> selectByParams(Integer position, Integer status){
        return companyAdMapper.selectByParams(position, status);
    }

    /**
     * 批量更新逛
     * @param companyAds
     * @return
     */
    public int batchUpdate(List<CompanyAd> companyAds){
        return companyAdMapper.batchUpdate(companyAds);
    }

    /**
     * 检查当前展示的广告位是否过期，并修改过期状态
     */
    public void checkAdStatus(){
        Date nowDate = new Date();
        List<CompanyAd> companyAds = companyAdMapper.selectAdWaitClose(nowDate);
        log.info("定时检查广告过期状态，当前过期广告{}个",companyAds.size());
        if(companyAds.size() > 0){
            List<Integer> positions = companyAds.stream().map(e->e.getPosition()).distinct().collect(Collectors.toList());
            List<ProjectInfo> projectInfos = projectInfoMapper.selectByPositions(positions);
            for(CompanyAd ad : companyAds){
                //广告位过期了需要返回项目设置的广告库存
                projectInfos.stream().filter(e->e.getPosition().equals(ad.getPosition())).forEach(item->{
                    item.setStock(item.getStock() == null? 0 : item.getStock() + 1);
                });
                ad.setStatus(BaseStatusEnum.INVALID.getValue());
            }
            companyAdMapper.batchUpdate(companyAds);
            projectInfoMapper.batchUpdate(projectInfos);
        }
    }
}
